// pages/manage/clue/clue.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    msgNumber:{
      handleNums:0,
      unhandleNums:0,
      totalNums:0
    },
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    screenHeight: app.globalData.screenHeight,
    userInfo:{},
    loanClueList:{}, // 对象 页数为key，防止多次触发触底造成数据重复现象
    searchParams:{
      pageNum:1,
      pageSize:6,//默认为6
      total:0, //总条数
      pages: 0, //总页数
      queryType: 3, //1全部 2已处理 3未处理
    },
    noData: false,
    statusOptions:{
      0:"已提交",
      1:"排单中",
      2:"排单中",
      3:"排单中",
      4:"排单中",
      5:"排单中",
      6:"排单中",
      7:"订单作废"
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    this.InitUserStatus();
  },
  // 检查一下登录状态。
  async InitUserStatus(){
      app.showLoading("获取用户信息");
      await app.checkToken();
      this.InitUserInfo();
      wx.hideLoading();
  },
  InitUserInfo(){
      // let userInfo = app.globalData.userInfo;
      this.goQuery();
      // 这里再验证一遍权限。
      // if (userInfo.roleList.indexOf('manageSecondBatch') == -1) {
      //     app.showNoAuthOrNoGrant('您无权操作查看此页面');
      //     return;
      // }else{
      //     this.goQuery();
      // }
  },
  switchType(e){
      let type = Number(e.currentTarget.dataset.type);
      let _this = this;
      this.setData({
          'searchParams.queryType': type
      },(res)=>{
          _this.goQuery();
      })
  },
  goQuery(){
    this.setData({
      'searchParams.pageNum':1,
      'searchParams.total':'',
      'searchParams.pages':'',
      'loanClueList': {}
    });
    this.custMagerCarLoanClueList(this.data.searchParams);
    this.custMagerCarLoanClueStat();
  },
  onReachBottom(){
      if (this.data.searchParams.pageNum == this.data.searchParams.pages) {
          //app.alert('没有更多了哦~');
      }else{
          let self = this;
          this.setData({
              'searchParams.pageNum': self.data.searchParams.pageNum+1
          });
          this.custMagerCarLoanClueList(this.data.searchParams);
      }
  },    
  custMagerCarLoanClueList(searchParams){
    app.sHttp.custMagerCarLoanClueList(searchParams).then(res=>{
        let loanClueList = this.data.loanClueList;
        res.result.dataList.forEach(item =>{
          item.loanMoney = (item.loanMoney/10000).toFixed(1);
        })
        loanClueList[searchParams.pageNum] = res.result.dataList;
        if (0 == res.result.total) {
          this.setData({
            'noData': true
          })
        }else{
            this.setData({
                'noData': false,
                'loanClueList': loanClueList,
                'searchParams.total':res.result.total,
                'searchParams.pages': res.result.pages
              });
        }
      },err=>{
        app.error(err.msg);
      })
  },
  custMagerCarLoanClueStat(){
    app.sHttp.custMagerCarLoanClueStat().then(res =>{
      this.setData({
        'msgNumber': res.result
      });
    },err=>{
    })
  },
  addcszt(){
    wx.navigateTo({
      url: '/pages/home/zhitui/add/add',
    })
  },
  openCustomerServiceChat(){
    app.openCustomerServiceChat();
  },
  seeCarMsg(e){
      let cid = e.currentTarget.dataset.cid;
      wx.navigateTo({
        url: '/pages/card/card?cid=' + cid,
      })
  },
  
  makePhoneCall(e){
    wx.makePhoneCall({
        phoneNumber: e.currentTarget.dataset.number,
        success:function(res){
            console.log(res);
        },
        fail:function(){}
    })
},
})